//
//  M_DetailJichuView.swift
//  letuofu
//
//  Created by dooland2 on 2020/6/4.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit
import CoreText

class M_DetailJichuView: UIView {

    var productInfo = [[String:Any?]]()
    let closeAllBtn = Button()
    let closeBtn = Button()
    let cottonView = UIView()
    let mainView = UIView()
    let textView = UITextView()
    
    func GetMallFreightIllustration(callback: @escaping ([String:Any?]) -> Void) {
        let params :[String:Any] = ["identityToken":LocalStorage.getString(forKey: USER_IDENTITYTOKEN) ?? "", "zoneCode": "105002"]
        API.request(url: .M_GetMallFreightIllustration, params: params) { (result) in
            if result.success {
               if let dl = result.data?.getDictionary("data") {
                   callback(dl)
               }
            }else{
            }
        }
    }
    
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
        drawView(view: closeAllBtn, { (make) in
            make.left.right.top.bottom.equalToSuperview()
        })
        closeAllBtn.backgroundColor = .init(white: 0, alpha: 0.6)
        closeAllBtn.addTarget(self, action: #selector(closeAllBtnClick(btn:)), for: .touchUpInside)
        
        drawView(view: mainView, { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(self.snp.bottom)
            make.height.equalTo(305)
        })
        mainView.backgroundColor = .white

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute:{
            self.showMainView()
        })
        
        mainView.drawLabel(text: "基础服务", fontSize: 15, textColor: .x333333, { (make) in
            make.top.equalToSuperview().offset(17)
            make.centerX.equalToSuperview()
        })
        
//        let img = mainView.drawImage(named: "bfuhe_80") { (make) in
//            make.left.equalToSuperview().offset(12)
//            make.top.equalToSuperview().offset(50)
//        }
        
        mainView.drawButton(button: closeBtn,title: "确定", titleColor: .white, fontSize: 15) { (make) in
            make.bottom.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(35)
        }
        closeBtn.backgroundColor = .xFC4C02
        closeBtn.layer.cornerRadius = 7
        closeBtn.addTarget(self, action: #selector(closeAllBtnClick(btn:)), for: .touchUpInside)
        
        GetMallFreightIllustration(callback: { (data) in
            self.mainView.drawView(view: self.textView, { (make) in
                make.centerX.equalToSuperview()
                make.right.equalToSuperview().offset(-10)
                make.top.equalTo(40)
                make.bottom.equalTo(self.closeBtn.snp.top).offset(-20)
            })
            self.textView.textColor = .x999999
            self.textView.font = .systemFont(ofSize: 14)
            self.textView.isEditable = false
            self.textView.isPagingEnabled = false
            var fuwuStr = data.getString("freightIllustration") ?? ""
            fuwuStr = fuwuStr.replacingOccurrences(of: ";", with: ";\n\n")
            fuwuStr = fuwuStr.replacingOccurrences(of: "。", with: "。\n\n")
            fuwuStr = fuwuStr.replacingOccurrences(of: "；", with: "；\n\n")
            fuwuStr = fuwuStr.replacingOccurrences(of: "！", with: "！\n\n")
            self.textView.text = fuwuStr
            self.textView.textAlignment = .left
        })
    }

    func showMainView() {
        UIView.animate(withDuration: 0.4) {
            // 平移
            self.mainView.transform = CGAffineTransform(translationX: 0, y: -self.mainView.frame.height)
        }
    }
    
    @objc func closeAllBtnClick(btn: Button) {
        UIView.animate(withDuration: 0.4, animations: {
            self.mainView.transform = CGAffineTransform(translationX: 0, y: 0)
        }, completion: { _ in
            self.removeFromSuperview()
        })
    }
}
